
package com.be.beadmin.product.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.product.dto.input.ProductProcessBomInforDto;
import com.be.beadmin.product.dto.output.ProductProcessBomInforEx;
import com.be.beadmin.product.entity.ProductProcessBomInfor;
import com.be.beadmin.product.entity.ProductProcessBomInfor_;
import com.be.beadmin.product.mapper.ProductProcessBomInforMapper;
import com.be.beadmin.product.mapper.ProductProcessBomInforMapperEx;
import com.be.beadmin.product.repository.ProductProcessBomInforRepository;
import com.be.beadmin.product.service.ProductProcessBomInforService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author zjm
* @date 2023-03-19
**/
@Service
@RequiredArgsConstructor
public class ProductProcessBomInforServiceImpl implements ProductProcessBomInforService {

    @Autowired
    private  ProductProcessBomInforRepository productProcessBomInforRepository;
    @Autowired
    private  ProductProcessBomInforMapper productProcessBomInforMapper;
    @Autowired
    private  ProductProcessBomInforMapperEx productProcessBomInforExMapper;

    @Override
    public Page<ProductProcessBomInforEx> getProductProcessBomInforByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<ProductProcessBomInfor> page = productProcessBomInforRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(ProductProcessBomInfor_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(ProductProcessBomInfor_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<ProductProcessBomInforEx> page1 = page.map(productProcessBomInforExMapper::toEntity);
            return page1;

    }

    @Override
    public void addProductProcessBomInfor(ProductProcessBomInforDto productProcessBomInforDto) {
            ProductProcessBomInfor result = productProcessBomInforRepository.findProductProcessBomInforByIdAndDelete(productProcessBomInforDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("ProductProcessBomInfor不存在,请重新查询");
            }
            ProductProcessBomInfor productProcessBomInfor = productProcessBomInforMapper.toEntity(productProcessBomInforDto);
            productProcessBomInforRepository.save(productProcessBomInfor);
    }

    @Override
    public void updateProductProcessBomInfor(ProductProcessBomInforDto productProcessBomInforDto) {
            ProductProcessBomInfor productProcessBomInfor = productProcessBomInforRepository.findProductProcessBomInforById(productProcessBomInforDto.getId());
            productProcessBomInfor = productProcessBomInforMapper.toEntity(productProcessBomInforDto);
            productProcessBomInforRepository.save(productProcessBomInfor);
    }


    @Override
    public void delProductProcessBomInfor(String id) {
        ProductProcessBomInfor productProcessBomInfor = productProcessBomInforRepository.findProductProcessBomInforById(id);
        productProcessBomInfor.setDelete(1);
        productProcessBomInforRepository.save(productProcessBomInfor);
    }

    @Override
    public void delProductProcessBomInfors (List<String> ids) { }
}